global.$ = $;

var abar = require('address_bar');
var folder_view = require('folder_view');
var fs = require('fs');

var shell = require('nw.gui').Shell;

var i =0;
//windows用户主目录
//var homepath = '~';
//var outFile = '/home/xux'; //Linux目录

$(document).ready(function() {
//  var folder = new folder_view.Folder($('#files'));
//  var addressbar = new abar.AddressBar($('#addressbar'));
//
//  folder.open(process.cwd());
//
//  addressbar.set(process.cwd());
//
//  folder.on('navigate', function(dir, mime) {
//    if (mime.type == 'folder') {
//      addressbar.enter(mime);
//    } else {
//      shell.openItem(mime.path);
//    }
//  });
//
//  addressbar.on('navigate', function(dir) {
//    folder.open(dir);
//  });

    $('#plus').bind('click',function(){
        addPlusFile();
    });
    $('#minus').bind('click',function(){});
    $('#btn-compression').bind('click',function(){
        compression();
    });
    $("#btn-close").bind('click',function(){
        window.close();
    });
});

/**
 * 压缩
 */
function compression(){
    var tag = '\\'; //windows
    var path = require('path');
    var t = new Array();
    //压缩
    var files = $("input[xux-file]");
    for(var a = 0;a < files.length;a++){
        var file = $('#'+files[a].id);
        var fs = file.val().split(';');
        for(var index = 0; index < fs.length;index++){
//            var a = fs[index].substr(0,fs[index].lastIndexOf(tag));
            var b = fs[index].substr(fs[index].lastIndexOf(tag)+1);
            if(b == '.'){
                continue;
            }
            //如果不是ceb文件 则跳过去
            if(!check(b)){
                continue;
            }
            var dir = path.dirname(fs[index]);
            t.push({source:fs[index],target:dir.substr(dir.lastIndexOf(tag)+1)+tag+b});
        }
    }
    var EasyZip = require('easy-zip').EasyZip;
    var zip = new EasyZip();
    //压缩
    zip.batchAdd(t,function(){
        var outFile = 'D:';
        zip.writeToFile(outFile+tag+'CEB-'+getonename()+'.zip',function (err) {
            //如果出错 则设置进度到到90%
            if (err){
                $("#div-progress").attr("class",'progress progress-danger progress-striped');
                $("div[class=bar]").css("width",90+"%");
                alert(err.message);
            }else{
                $("div[class=bar]").css("width",100+"%");
                window.close();
            }
        });
    });
    progressRun();
}
/**
 * 添加按钮
 */
function addPlusFile(){
    var form = $('#content');
    i++;
    form.append($('<div class="control-group" id="'+i+'-div">'+
                    '<label class="control-label" >CEB文件夹</label>'+
                        '<div class="controls">'+
                            '<input type="file" id="'+i+'file" xux-file=true  webkitdirectory value="选择文件夹">'+
                            '<a class="btn plus" href="#" onclick="addPlusFile()"><i class="icon-plus"></i></a>'+
                            '<a class="btn" href="#"  onclick="removeFile('+i+')"><i class="icon-minus"></i></a>'+
                            '</div>'+
                    '</div>'));
}
/**
 * 删除文件框
 * @param id
 */
function removeFile(id){
    $('#'+id+'-div').remove();
}
var p = 0;
/**
 * 进度条
 */
function progressRun(){
    p+=5;
    $("div[class=bar]").css("width",p+"%");
    if(p<100){
        var timer=setTimeout("progressRun()",1000);
    }
}

/**
 * 检查文件名后缀是否是ceb文件
 * @param s
 * @returns {boolean}
 */
function check(s)
{
    var i = s.lastIndexOf(".");
    if(i < 0)
    {
        return false;
    }

    var var1 = s.substring(i+1) ;
    if(var1 != "ceb")
    {
        return false;
    }
    return true ;
}
/**
 * 生成文件名
 * @returns {string}
 */
function getonename(){
    var word="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    var subn="";
    var subn2="";
    var today=new Date();
    subn2+=today.getFullYear();
    subn2+=today.getMonth()+1;
    subn2+=today.getDate();
    subn2+=today.getHours();
    subn2+=today.getMinutes();
    subn2+=today.getSeconds();
    for(i=0; i<3;i++){
        subn+=word.substr(Math.floor(Math.random()*word.length),1);
    }
    return subn2+subn;
}